<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/19 0019
 * Time: 16:45
 */

namespace app\common\service;


use think\Db;
use think\Exception;

class CommentsService extends Service
{
    protected $tableName = 'sms_mk_comments';

    /**
     * @param $view
     * @param $params
     * @param int $isdelete  返回全部商品分页
     */
    public function searchPage(&$view,$params,$isdelete = 0){

        $this->db->where('c.isdelete','=',$isdelete)
            ->alias('c')
            ->join('orders_view o','o.id = c.sms_orders_id')
            ->field('c.*,o.name,o.link_name')
            ->order('c.create_time','desc');

        $storeId = $this->checkStoreSelect(@$params['store_id']);
        if($storeId) $this->db->where('c.sms_stores_id','=',$storeId);
        if(@$params['name']) $this->db->where('o.name','like','%'.$params['user_name'].'%');

        if(@$params['name']) $this->db->where('c.comment','like','%'.$params['comment'].'%');

        $this->pageList($view,$params);
    }

    public function datalist()
    {
        $this->db->alias('c')
            ->join('sms_mk_users u','u.id = c.sms_users_id')
            ->where('c.sms_stores_id','=',@$this->param['store_id'])
            ->field('c.*,u.phone,u.name as user_name')
            ->group('sms_orders_id');

        $result =  parent::datalist(); // TODO: Change the autogenerated stub
        foreach ($result['data'] as $v=>$value){
            $photos = explode(',',$result['data'][$v]['photos']);
            if($photos)
                $result['data'][$v]['photos'] = Db::table('sms_file')->whereIn('id',$photos)->select();
        }
        return $result;
    }

    /**
     * 添加订单评论
     */
    public function order_add(){
        if(empty($this->param['order_id'])) throw new Exception('请指定订单');
        //状态修改
        $oService = new OrdersService($this->param);
        $oService->editState(@$this->param['order_id'],'comment','finished',true);
        $orderInfo = $oService->getOrderGoods($this->param['order_id']);
        foreach (@$orderInfo['goods'] as $g=>$goods){
            $data[$g]['comment'] = $this->param['comment'];
            if($this->param['photos'])
                $data[$g]['photos'] = join(',',$this->param['photos']);
            $data[$g]['sms_goods_id'] = $goods['id'];
            $data[$g]['sms_users_id'] = $this->param['sms_users_id'];
            $data[$g]['isdelete'] = 0;
            $data[$g]['create_time'] = (string)time();
            $data[$g]['star'] = $this->param['star'];
            $data[$g]['sms_order_id'] = $this->param['order_id'];
            $data[$g]['sms_stores_id'] = $goods['sms_stores_id'];
        }
        if(@$data)
            $this->db->insertAll($data);
    }

    public function comment_add()
    {
        $data['comment'] = $this->param['comment'];
        if($this->param['photos'])
            $data['photos'] = join($this->param['photos'],',');
        $data['sms_goods_id'] = $this->param['id'];
        $data['sms_users_id'] = $this->param['sms_users_id'];
        $data['isdelete'] = 0;
        $data['star'] = $this->param['star'];

        return $this->db->insertGetId($data);
    }

}